package org.sixdog.mcsit.service.impl;

import org.apache.ibatis.annotations.Param;
import org.sixdog.mcsit.dao.UserDao;
import org.sixdog.mcsit.po.User;
import org.sixdog.mcsit.service.UserService;
import org.sixdog.mcsit.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public boolean insterUser(User user){
        if (StringUtils.isBlank(user.getUid())){
            user.setUid(UUID.randomUUID().toString());
        }
        Date date = new Date();
        if (user.getRegisterTime()==null){
            user.setRegisterTime(date);
        }
        if (user.getUpdateTime()==null){
            user.setUpdateTime(date);
        }

        int i = userDao.insterUser(user);
        if (i==1){
            return true;
        }
        return false;
    }
    @Override
    public boolean updateUser(User user){
        int i = userDao.updateUser(user);
        if (i==1){
            return true;
        }
        return false;
    }
    @Override
    public  boolean deleteUserByUid(String uid){
        int i = userDao.deleteUserByUid(uid);
        if (i==1){
            return true;
        }
        return false;
    }
    @Override
    public User selectUserByUid(String uid){
        User user = userDao.selectUserByUid(uid);
        return user;
    }
    @Override
    public List<User> selectUsers(User user,int pageNumber, int pageSize){
        int pageStart = (pageNumber-1)*pageSize;
        List<User> users = userDao.selectUsers(user, pageStart, pageSize);
        return users;
    }

    @Override
    public User UserLogin(String email,String password) {
        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        user.setStatus(1);
        List<User> users = userDao.selectUsers(user, 0, 1);
        if (users.size()==0){
            return null;
        }
        User logingUser = users.get(0);
        return logingUser;
    }

    @Override
    public boolean updateUserByEmail(User user) {
        int i = userDao.updateUserByEmail(user);
        if (i>0){
            return true;
        }
        return false;
    }

}
